const Topic = require("../model/topic.model");
const User = require("../model/user.model");
const Comment = require("../model/comment.model");
class TopicService {
  async createTopic(topic_detail) {
    const res = await Topic.create(topic_detail);
    return res.dataValues;
  }
  async updateTopicUser(id, user_name, user_avatar) {
    const res = await Topic.update(
      {
        topic_user_name: user_name,
        topic_user_avatar: user_avatar,
      },
      {
        where: {
          topic_user_id: id,
        },
      }
    );
  }

  async updateCommentInfo(
    comment_user_id,
    comment_user_name,
    comment_user_avatar
  ) {
    const res = await Comment.update(
      {
        comment_user_name: comment_user_name,
        comment_user_avatar: comment_user_avatar,
      },
      {
        where: {
          comment_user_id,
        },
      }
    );
  }

  async getAllTopics() {
    const res = await Topic.findAll({
      attributes: [
        "id",
        "topic_user_id",
        "topic_user_name",
        "topic_user_avatar",
        "topic_type",
        "topic_title",
        "topic_content",
        "topic_img",
        "topic_number_of_likes",
        "updatedAt",
        "createdAt",
      ],
    });
    return res ? res : null;
  }

  async postMyComment(commentMes) {
    const res = await Comment.create(commentMes);
    return res.dataValues;
  }

  async getComments(comment_topic_id) {
    const res = await Comment.findAll({
      where: {
        comment_topic_id,
      },
    });
    /*   console.log("+++++++++++++++++++++++++++++++++++++++++");
    console.log(res);
    console.log("----------------------------------------"); */
    return res ? res : null;
  }

  async addLikes(ctx) {
    let likes = await Topic.findOne({
      attributes: ["topic_number_of_likes"],
      where: {
        id: ctx,
      },
    });

    likes = parseInt(likes.dataValues.topic_number_of_likes) + 1;

    const res = await Topic.update(
      {
        topic_number_of_likes: likes,
      },
      {
        where: {
          id: ctx,
        },
      }
    );

    return res ? res.dataValues : null;
  }

  async disLikes(ctx) {
    console.log("id", ctx);
    let likes = await Topic.findOne({
      attributes: ["topic_number_of_likes"],
      where: {
        id: ctx,
      },
    });

    likes = parseInt(likes.dataValues.topic_number_of_likes) - 1;
    const res = await Topic.update(
      {
        topic_number_of_likes: likes,
      },
      {
        where: {
          id: ctx,
        },
      }
    );

    return res ? res.dataValues : null;
  }

  async deleteComments(topic_id) {
    const res = await Comment.destroy({
      where: {
        comment_topic_id: topic_id,
      },
    });
    return res.dataValues;
  }
  async deleteTopic(topic_id) {
    const res = await Topic.destroy({
      where: {
        id: topic_id,
      },
    });
    return res.dataValues;
  }
}

module.exports = new TopicService();
